﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using BaseDll;
namespace UserData
{
    public partial class Form_TrayManage : UIPage
    {
        TrayData trayData = null;
        int n_OldRow = 0;
        int n_OldColumn = 0;
        public Form_TrayManage(string strName)
        {
            InitializeComponent();
            if(TrayDataMgr.GetInstance().dicTrayData.ContainsKey(strName))
            {
                this.trayData = TrayDataMgr.GetInstance().dicTrayData[strName];
                tool_Name.Text = strName;
                this.Text = strName;
                trayData.OnTestLoadTrayData += TrayData_OnTestLoadTrayData;
                TrayData_OnTestLoadTrayData(trayData.RowCount, trayData.ColCount);
                trayData.OnTestChangeCellIndex += TrayData_OnTestChangeCellIndex;
            }
            else
            {
                MessageBox.Show($"尚未配置料盘{strName}");
            }
        }

        private void TrayData_OnTestChangeCellIndex(int n_Row, int n_Column)
        {
            ReflushUI(n_Row, n_Column);
        }

        private void TrayData_OnTestLoadTrayData(int n_Row, int n_Column)
        {
            table_Tray.RowStyles.Clear();
            table_Tray.ColumnStyles.Clear();
            table_Tray.Controls.Clear();
            table_Tray.RowCount = (int)n_Row;
            table_Tray.ColumnCount = (int)n_Column;
            for (int i = 0; i < n_Row; i++)
            {
                table_Tray.RowStyles.Add(new RowStyle(SizeType.Percent, (float)1.0 / n_Row));
                for (int j = 0; j < n_Column; j++)
                {
                    table_Tray.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)1.0 / n_Column));
                    UIPanel panel = new UIPanel();
                    panel.Dock = DockStyle.Fill;
                    panel.FillColor = Color.Gray;
                    panel.Text = (j + i * n_Column + 1).ToString();
                    table_Tray.Controls.Add(panel, j, i);
                    panel.DoubleClick += Panel_DoubleClick;
                }
            }
            ReflushUI(0, 0);
        }

        private void Panel_DoubleClick(object sender, EventArgs e)
        {
            UIPanel btn = (UIPanel)sender;
            int nRow = (int.Parse(btn.Text) - 1) / trayData.ColCount;
            int nColumn = (int.Parse(btn.Text) - 1) % trayData.ColCount;
            trayData.RowNow = nRow;
            trayData.ColNow = nColumn;
            ReflushUI(nRow, nColumn);
        }

        private void ReflushUI(int nRow, int nColumn)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ReflushUI(nRow,nColumn)));
            }
            else
            {
                if (table_Tray.Controls.Count == 0)
                    return;
               ((UIPanel) table_Tray.Controls[n_OldRow * trayData.ColCount + n_OldColumn]).FillColor = Color.Gray;
               ((UIPanel) table_Tray.Controls[nRow * trayData.ColCount + nColumn]).FillColor = Color.Green;
                n_OldRow = nRow;
                n_OldColumn = nColumn;
            }
        }
        private void tool_Reset_Click(object sender, EventArgs e)
        {
            if(UIMessageBox.ShowAsk($"是否重置料盘:{ tool_Name.Text }的行数列数?"))
            {
                trayData.SetRowAndColumn(0, 0);
            }
        }
    }
}
